package display;

import option.Data;
import option.Sweeper;
import option.Time;
import paihang.Rank;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class BodyPanel extends JPanel {
    private JButton[][] buttons;/*网格按钮二维数组*/
    private JLabel sweepercount = new JLabel("方格比例：" + Data.x + "*" + Data.y + "       总雷数：" + Data.leiCount);
    private Thread t;/*线程*/
    public static Time time;/*修改已用时间*/

    public BodyPanel() {

        buttons = new JButton[Data.x][Data.y];/*建立x*y数组对象*/
        this.setBackground(new Color(201, 226, 252));/*设置主体背景*/
        JPanel top = new JPanel() {
            @Override
            public void paintComponent(Graphics g) {
                g.drawImage(Toolkit.getDefaultToolkit().getImage("src/image/MAEU.jpg"), 0, -40, 90 + 35 * Data.x, 70, this);
            }
        };/*建立容器显示记录时间*/
        top.setBackground(new Color(201, 226, 252));
        JPanel bottom = new JPanel();/*底部容器显示非雷数*/

        top.setPreferredSize(new Dimension(100 + 35 * Data.x, 30));/*设置容器长宽*/

        bottom.setBackground(new Color(201, 226, 252));

        sweepercount.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        sweepercount.setOpaque(false);
        bottom.add(sweepercount);

        JLabel timeLabel = new JLabel();/*创建时间标签*/
        timeLabel.setText("已用时间：0.00秒");
        timeLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20));

        top.add(timeLabel);/*将时间标签加入容器中*/

        time = new Time(timeLabel);/*创建修改已用时间类*/
        t = new Thread(time);/*创建线程*/

        JPanel body = new JPanel();
        body.setBackground(Color.white);
        body.setPreferredSize(new Dimension(Data.x * 36 + 10, Data.y * 36 + 10));
        body.setBorder(BorderFactory.createLineBorder(new Color(150, 200, 200), 2));
        body.setLayout(null);
        Sweeper.initSweeper();/*雷数据初始化*/
        /*添加button按钮*/
        for (int i = 0; i < Data.x; i++) {
            for (int j = 0; j < Data.y; j++) {
                JButton button = new JButton();
                buttons[i][j] = button;
                button.setLocation(5 + i * 36, 5 + j * 36);
                button.setSize(new Dimension(35, 35));
                button.setFont(new Font("微软雅黑", Font.PLAIN, 20));
                button.setBorder(BorderFactory.createLineBorder(new Color(201, 226, 252), 1));
                button.setBackground(new Color(135, 206, 235));
                /*加入焦点获取*/
                button.setFocusable(true);
                button.addFocusListener(new FocusAdapter() {
                    @Override
                    public void focusGained(FocusEvent e) {
                        Time.i = 0.014;
                    }

                    @Override
                    public void focusLost(FocusEvent e) {
                        Time.i = 0;
                    }
                });
                /*给button添加鼠标右键、中键监听器，用于插旗、作弊器*/
                button.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mousePressed(MouseEvent e) {
                        super.mouseClicked(e);
                        JButton button = (JButton) e.getSource();
                        if (e.getButton() == MouseEvent.BUTTON3) {
                            if (button.isEnabled() && button.getBackground() == Color.black) {
                                button.setBackground(new Color(135, 206, 235));
                            } else if (button.isEnabled() && button.getBackground() != Color.BLACK) {
                                button.setBackground(Color.BLACK);
                            }
                        }

                        if (e.getButton() == MouseEvent.BUTTON2) {
                            int i, j = 0, flag = 0;
                            for (i = 0; i < Data.x; i++) {
                                for (j = 0; j < Data.y; j++) {
                                    if (buttons[i][j] == button) {
                                        flag = 1;
                                        break;
                                    }
                                }
                                if (flag == 1) break;
                            }
                            if (button.getBackground() != Color.BLACK && Sweeper.isVisited[i][j] == 0) {
                                if (Sweeper.sweeper[i][j] == -1)
                                    buttons[i][j].setBackground(Color.red);

                                else if (Sweeper.sweeper[i][j] == 0)
                                    buttons[i][j].setBackground(Color.white);
                                else {
                                    buttons[i][j].setBackground(Color.white);
                                    buttons[i][j].setText(String.valueOf(Sweeper.sweeper[i][j]));
                                }
                            }


                        }

                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {
                        JButton button = (JButton) e.getSource();

                        if (e.getButton() == MouseEvent.BUTTON2) {
                            int i, j = 0, flag = 0;
                            for (i = 0; i < Data.x; i++) {
                                for (j = 0; j < Data.y; j++) {
                                    if (buttons[i][j] == button) {
                                        flag = 1;
                                        break;
                                    }
                                }
                                if (flag == 1) break;
                            }
                            if (button.getBackground() != Color.BLACK && Sweeper.isVisited[i][j] == 0) {
                                button.setBackground(new Color(135, 206, 235));
                                button.setText("");
                            }
                        }
                    }
                });
                /*给button添加事件监听器，打开方格*/

                button.addActionListener(new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (!time.isStart) {
                            t.start();
                        }
                        int i, j = 0, flag = 0;
                        JButton button = (JButton) e.getSource();
                        if (button.getBackground() == Color.BLACK) return;
                        /*查找对应按钮下标*/
                        for (i = 0; i < Data.x; i++) {
                            for (j = 0; j < Data.y; j++) {
                                if (buttons[i][j] == button) {
                                    flag = 1;
                                    break;
                                }
                            }
                            if (flag == 1) break;
                        }

                        int number = Sweeper.sweeper[i][j];/*该方格对应数字*/
                        if (number != -1) {/*如果不为雷*/
                            unfoldButton(i, j);
                            isWin();
                        } else {
                            /*中断线程*/
                            time.ifFinished = true;
                            //若为雷，打开全部格子
                            button.setBackground(Color.red);
                            button.setEnabled(false);
                            /*遍历全部格子并打开*/
                            for (int x = 0; x < Data.x; x++) {
                                for (int y = 0; y < Data.y; y++) {
                                    switch (Sweeper.sweeper[x][y]) {
                                        case 0:
                                            buttons[x][y].setBackground(Color.white);
                                            break;
                                        case -1:
                                            buttons[x][y].setIcon(new ImageIcon("src/image/雷1.jpg"));
                                            break;
                                        default:
                                            buttons[x][y].setBackground(Color.white);
                                            buttons[x][y].setText(String.valueOf(Sweeper.sweeper[x][y]));
                                    }
                                    buttons[x][y].setEnabled(true);
                                }
                            }
                            //显示新游戏提示
                            int value = JOptionPane.showConfirmDialog(
                                    BodyPanel.this,
                                    "你输了！是否开始新游戏？",
                                    "结果",
                                    JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE
                            );
                            if (value == 0) Sweeper.newGame();
                        }
                    }
                });
                body.add(button);
            }
        }
        this.add(top);
        this.add(body);
        this.add(bottom);
    }

    /**
     * 打开非雷区,深度优先遍历
     */
    public void unfoldButton(int i, int j) {
        if (Sweeper.isVisited[i][j] == 1) return;
        switch (Sweeper.sweeper[i][j]) {
            case -1:
                return;
            case 0:
                buttons[i][j].setBackground(Color.white);
                buttons[i][j].setEnabled(false);
                Sweeper.isVisited[i][j] = 1;
                Sweeper.count++;
                break;
            default:
                buttons[i][j].setBackground(Color.white);
                buttons[i][j].setText(String.valueOf(Sweeper.sweeper[i][j]));
                buttons[i][j].setEnabled(false);
                Sweeper.isVisited[i][j] = 1;
                Sweeper.count++;
                return;
        }
        for (int k = 0; k < 8; k++) {
            int x = i + Sweeper.directions[0][k];
            int y = j + Sweeper.directions[1][k];
            if (x >= 0 && x < Data.x && y >= 0 && y < Data.y) {
                unfoldButton(x, y);
            }
        }
    }

    /**
     * 检查是否胜利，打开雷区并提示新游戏
     */
    public void isWin() {
        if (Sweeper.count != Data.x * Data.y - Data.leiCount) return;
        for (int x = 0; x < Data.x; x++) {
            for (int y = 0; y < Data.y; y++) {
                if (Sweeper.sweeper[x][y] == -1) {
                    buttons[x][y].setIcon(new ImageIcon("src/image/雷1.jpg"));
                    buttons[x][y].setBackground(Color.green);
                }
            }
        }
        time.ifFinished = true;/*终止线程*/
        Rank.luru();
        int value = JOptionPane.showConfirmDialog(
                BodyPanel.this,
                "恭喜！你赢了！是否开始新游戏？",
                "结果",
                JOptionPane.YES_NO_OPTION
        );
        if (value == 0) Sweeper.newGame();
    }
}
